package online.flowerinsnow.minecraft.bukkit.floweress.command

import online.flowerinsnow.minecraft.bukkit.floweress.message.MessageConfig
import online.flowerinsnow.minecraft.bukkit.floweress.util.CommandUtils
import online.flowerinsnow.minecraft.bukkit.floweress.util.MessageUtils
import org.bukkit.Bukkit
import org.bukkit.command.Command
import org.bukkit.command.CommandSender
import org.bukkit.command.TabExecutor

class FlowerAIRadarAntiCheatCommand : TabExecutor {
    override fun onCommand(sender: CommandSender, command: Command, alias: String, args: Array<out String>): Boolean {
        when {
            args.size > 1 -> {
                val va = StringBuilder()
                for (i in 1 until args.size) {
                    va.append(args[i])
                    if (i + 1 < args.size) va.append(" ")
                }
                when (args[0].lowercase()) {
                    "fackick" -> {
                        if (CommandUtils.checkPermission(sender, "flowerairadaranticheat.fackick")) return true
                        val player = Bukkit.getPlayerExact(args[0])
                        if (player == null) {
                            sender.sendMessage(MessageConfig.TARGETNOTONLINE.getMessage())
                            return true
                        }
                        player.kickPlayer(MessageConfig.FAC_FACKICK_KICK.getMessage()
                                .replace("%(REASON)", va.toString()))
                        Bukkit.broadcastMessage(MessageConfig.FAC_FACKICK_BROADCAST.getMessage()
                                .replace("%(PLAYER)", player.name))
                    }
                    "notify1" -> {
                        if (CommandUtils.checkPermission(sender, "flowerairadaranticheat.notify")) return true
                        val player = Bukkit.getPlayerExact(args[0])
                        if (player == null) {
                            sender.sendMessage(MessageConfig.TARGETNOTONLINE.getMessage())
                            return true
                        }
                        Bukkit.getOnlinePlayers().forEach { p ->
                            if (p.hasPermission("flowerairadaranticheat.receivenotify")) {
                                p.sendMessage(MessageConfig.FAC_NOTIFY_I.getMessage()
                                        .replace("%(PLAYER)", player.name)
                                        .replace("%(CHEAT)", MessageUtils.parseColor(va.toString())))
                            }
                        }
                    }
                    "notify2" -> {
                        if (CommandUtils.checkPermission(sender, "flowerairadaranticheat.notify")) return true
                        val player = Bukkit.getPlayerExact(args[0])
                        if (player == null) {
                            sender.sendMessage(MessageConfig.TARGETNOTONLINE.getMessage())
                            return true
                        }
                        Bukkit.getOnlinePlayers().forEach { p ->
                            if (p.hasPermission("flowerairadaranticheat.receivenotify")) {
                                p.sendMessage(MessageConfig.FAC_NOTIFY_II.getMessage()
                                        .replace("%(PLAYER)", player.name)
                                        .replace("%(CHEAT)", MessageUtils.parseColor(va.toString())))
                            }
                        }
                    }
                    "owkick" -> {
                        if (CommandUtils.checkPermission(sender, "flowerairadaranticheat.owkick")) return true
                        val player = Bukkit.getPlayerExact(args[0])
                        if (player == null) {
                            sender.sendMessage(MessageConfig.TARGETNOTONLINE.getMessage())
                            return true
                        }
                        player.kickPlayer(MessageConfig.FAC_OWKICK_KICK.getMessage()
                                .replace("%(REASON)", va.toString()))
                        Bukkit.broadcastMessage(MessageConfig.FAC_OWKICK_BROADCAST.getMessage()
                                .replace("%(PLAYER)", player.name))
                    }
                    else -> {
                        sendUsage(sender)
                    }
                }
            }
            else -> {
                sendUsage(sender)
            }
        }
        return true
    }

    override fun onTabComplete(sender: CommandSender, command: Command, alias: String, args: Array<out String>): MutableList<String> {
        return when (args.size) {
            1 -> {
                val list = ArrayList<String>(listOf("fackick", "notify1", "notify2", "owkick"))
                list.removeIf { s -> !s.startsWith(args[0], true) }
                list
            }
            2 -> {
                val list = ArrayList<String>()
                Bukkit.getOnlinePlayers().forEach { p -> list.add(p.name) }
                list.removeIf { s -> !s.startsWith(args[1], true) }
                list
            }
            else -> ArrayList()
        }
    }

    private fun sendUsage(sender: CommandSender) {
        sender.sendMessage(MessageConfig.FAC_USAGE.getMessage())
    }
}